package org.leetcode.easy.leetcode234;

import org.algorithm.List.ListNode;

import java.util.*;

public class Solution
{
    class ListNode {
        int val;
        ListNode next;
        public ListNode (int val){
            this.val=val;
        }

        public ListNode(int val,ListNode next){
            this.val=val;
            this.next=next;
        }
    }



    public boolean isPalindrome2(ListNode head){

        if (head.next==null){
            return true;
        }

        List<Integer> list= new ArrayList<>();

        while (head!=null){
           list.add(head.val);
            head=head.next;
        }

        int left = 0;
        int right = list.size()-1;

        while (left<right){
            if (list.get(left)!=list.get(right)){
                return false;
            }

            left++;
            right--;
        }
        return true;
    }

    //将链表中的数据存放到双端队列中，判断双端队列中队首和队尾是否相等，最终根据队列的长度来判断是否是回文。
    public boolean isPalindrome(ListNode head) {

        Deque<Integer> deque = new LinkedList<>();

        while (head!=null)
        {
            deque.add(head.val);
            head= head.next;
        }

        int loopTime = deque.size()/2;

        while (loopTime>0)
        {
            if (deque.peekLast()== deque.peekFirst())
            {
                deque.pollFirst();
                deque.pollLast();
                loopTime--;
            }else {
                return false;
            }
        }

        if (deque.size()>1)
        {
            return false;
        }else {
            return true;
        }

    }

    public static void main(String[] args) {
//        ListNode listNode = new ListNode(1, new ListNode(0, new ListNode(1)));
//
//        Solution solution = new Solution();
//        boolean isPalindrome = solution.isPalindrome(listNode);
//
//        System.out.println(isPalindrome);
//
//        Deque<Integer> deque = new LinkedList<>();
//
//        deque.add(1);
//        deque.add(2);
//        deque.add(2);
//        deque.add(1);
//
//        int size = deque.size();
//
//        int loopTime = size/2;
//
//        while (loopTime>0)
//        {
//            if (deque.peekLast()== deque.peekFirst())
//            {
//                deque.pollFirst();
//                deque.pollLast();
//                loopTime--;
//            }else {
//                return;
//            }
//        }
//
//        System.out.println(deque.size());

    }
}
